package com.wigem.fa.controller;

import com.wigem.fa.entity.FaApplyEntity;
import com.wigem.fa.entity.FaEntInfoEntity;
import com.wigem.fa.service.FaApplyService;
import com.wigem.fa.service.FaEntInfoService;
import com.wigem.fa.vo.FaApplyVo;
import com.wigem.framework.common.pojo.PageResult;
import com.wigem.system.entity.SystemUserEntity;
import com.zqlian.sdk.data.center.common.credential.Credential;
import com.zqlian.sdk.data.center.common.exception.ZqlianSdkException;
import com.zqlian.sdk.data.center.common.profile.ClientProfile;
import com.zqlian.sdk.data.center.common.profile.HttpProfile;
import com.zqlian.sdk.data.center.common.utils.HttpMethod;
import com.zqlian.sdk.data.center.gateway.ApiGatewayRequest;
import com.zqlian.sdk.data.center.gateway.ApiGatewaySdkClient;
import lombok.extern.slf4j.Slf4j;
import com.wigem.system.service.SystemUserService;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class JingCeTest{
    @Resource
    private static FaApplyService faApplyService;

    @Resource
    private static FaEntInfoService entInfoService;
    public static void main(String[] args) {
//        测试环境：
//        政务外网http://172.26.64.155:8081/mp/api-gw-server/v1
//        互联网 https://103.83.44.92:8443/mp/api-gw-server/v1
//        正式环境：
//        政务外网 http://172.26.64.155:8080/mp/api-gw-server/v1
//        互联网 http://103.83.44.92/mp/api-gw-server/v1
        // 设置客户端配置
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setDomain("172.26.64.155:8081");
        clientProfile.setPrefixPath("/mp/api-gw-server/v1");
        clientProfile.setDebug(false);// 设置是否输出请求和响应相关信息以及SDK配置信息，默认false
        clientProfile.setDebugSSL(false);//设置是否忽略证书。默认false。建议仅在本地调试时设置为true
        // 设置请求相关默认配置
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setProtocol(HttpProfile.REQ_HTTP);//更改请求协议。默认Http
        httpProfile.setRequestMethod(HttpMethod.GET.name());// 更改默认请求方法。默认GET
        httpProfile.setConnectTimeoutSec(3);//设置连接超时，单位秒。默认3。0为无限等待，不能小于0。
        httpProfile.setReadTimeoutSec(3);//设置读超时，单位秒。默认3。0为无限等待，不能小于0。
        httpProfile.setWriteTimeoutSec(3);//设置写超时，单位秒。默认3。0为无限等待，不能小于0。
        clientProfile.setHttpProfile(httpProfile);
        // 设置AK和AS
//        测试环境：
//        AccessKey：Ki3vfj986r
//        AccessSecret：IPBQ0N1AMm1xZKFm
//        正式环境：
//        AccessKey：oonnV2kTJ2
//        AccessSecret：aa53YZxQpqwgf5OY
        Credential credential = new Credential();
        credential.setAccessKey("Ki3vfj986r");
        credential.setAccessSecret("IPBQ0N1AMm1xZKFm");
        // apiVersion没有则可以不指定
        ApiGatewaySdkClient client = new ApiGatewaySdkClient("v1.0", credential, clientProfile);

        FaApplyVo faApplyVo=new FaApplyVo();
        faApplyVo.setApplyId("2");//初审退回修改
        PageResult<FaApplyEntity> list = faApplyService.getPageList(faApplyVo);
        //组装请求
        Map<String, Object> param = new HashMap<>();
        param.put("batchNo", "report2024new");//当前报告期
        param.put("dataSize", 7);
        param.put("reportTime", "20241021145304");

        List<Map<String, Object>> dataList = new ArrayList<>();
        Map<String, Object> dataItem = new HashMap<>();
        for (FaApplyEntity faApplyEntity : list.getList()) {
            dataItem = new HashMap<>();
            dataItem.put("requestId", faApplyEntity.getApproveId());//申请ID
            dataItem.put("requestName", faApplyEntity.getProjectName());//项目名称
            dataItem.put("projectId", faApplyEntity.getPolicyType());//政策类型
            dataItem.put("regionCode", "110105");
            dataItem.put("applyerDate", faApplyEntity.getSubmitTime());
            dataItem.put("applyerMoney", faApplyEntity.getProposalFund());
            dataItem.put("requestState", "new");
            dataItem.put("fromData", "项目介绍："+faApplyEntity.getProjectIntroduction());
            dataItem.put("applyerType", "1");//法人
            FaEntInfoEntity entity = entInfoService.getEntity(faApplyEntity.getEntId());
            dataItem.put("applyeyName", entity.getOrgName());
            dataItem.put("applyerPageType","001");//类型：社会信用代码
            dataItem.put("applyerPageCode", entity.getSocialCreditCode());//社会信用代码
            dataItem.put("cdOperation", "I");
            dataList.add(dataItem);
        }
        param.put("data", dataList);
        //测试：cLpkrGnmKA  生产：d0gTFl1HLW
        String serviceId="cLpkrGnmKA";
        ApiGatewayRequest request = new ApiGatewayRequest(param, serviceId);
        request.setRequestId("");// 请求ID，可以不指定，使用客户端自动生成的requestId
        try {
            //用法1：直接获取json字符串
            String exchange = client.exchange(request, HttpMethod.POST);// 单独指定HttpMethod
            System.out.println(exchange);
        } catch (ZqlianSdkException e) {
            throw new RuntimeException(e);
        }
//      用法2：返回确定的实体类型
//            TypeReference<JsonResponseModel<DataWarehousePagination<DataWarehousePolicyVO>>> typeReference
//                    = new TypeReference<JsonResponseModel<DataWarehousePagination<DataWarehousePolicyVO>>>() {};
//            JsonResponseModel<DataWarehousePagination<DataWarehousePolicyVO>> exchange = client.exchange(request, null, typeReference);//HttpMethod为null时则使用默认HttpMethod
//            log.info("{}", JsonUtil.toJson(exchange));
//        } catch (ZqlianSdkException e) {
//            log.error(e.getMessage(),e);// 处理SDK异常
//        } catch (JsonProcessingException e) {
//            log.error(e.getMessage(),e);
//        }
    }


}
